package com.bmw.accesscount.service.impl;

import com.bmw.accesscount.dao.ROrderDao;
import com.bmw.accesscount.model.ROrder;
import com.bmw.accesscount.service.ROrderService;
import com.bmw.accesscount.util.bean.OrderReq;
import com.bmw.accesscount.util.redis.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


/**
 * Classname: ROrderServiceImpl
 * Package: com.bmw.accesscount.service.impl
 * Description:
 *
 * @Author 余集楠
 * @Create 2024/6/12 0012 21:48
 * @Version 1.8
 */
@Service
@Slf4j
@Transactional
public class ROrderServiceImpl implements ROrderService {
    @Autowired
    ROrderDao rOrderDao;

    @Autowired
    RedisUtil redisUtil;

    @Override
    public int order(OrderReq orderReq) {
        //保存到数据库
        ROrder rOrder=new ROrder();
        rOrder.setUserId(orderReq.getUserId());
        rOrder.setNewsName(orderReq.getNewsName());
        rOrder.setIsDeleted(Integer.valueOf(0));
        rOrder.setCreateTime(new Date());
        int i=rOrderDao.insert(rOrder);
        if(i>0){
            //更新缓存中的排行榜的数据
            String key="rank:list";
            redisUtil.zincrby(key, orderReq.getNewsName(), Double.valueOf(i));
        }
        return i;
    }
}
